package com.example.newheze;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.example.newheze.Camera.CameraActivity;
import com.example.newheze.CancelAnima.BottomNavigationHelper;
import com.example.newheze.Current.CurrentActivity;
import com.example.newheze.FieldMessage.AnalogData;
import com.example.newheze.FieldMessage.MessageActivity;
import com.example.newheze.User.UserActivity;
import com.example.newheze.Util.HttpUtils;
import com.example.newheze.vo.Dtu;
import com.example.newheze.vo.Info;
import com.example.newheze.vo.RemoteRecycleFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jzvd.Jzvd;

public class MainActivity extends MyActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {


        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    setTitleName(getResources().getString(R.string.title_home));
                    CurrentActivity currentActivity = new CurrentActivity("006");
                    fragmentTransaction.replace(R.id.fragment, currentActivity);
                    fragmentTransaction.commit();

                    return true;
                case R.id.navigation_dashboard:
                    setTitleName(getResources().getString(R.string.title_dashboard));
                    MessageActivity messageActivity = new MessageActivity();
                    fragmentTransaction.replace(R.id.fragment, messageActivity);
                    fragmentTransaction.commit();

                    return true;
                case R.id.navigation_notifications:
                    setTitleName(getResources().getString(R.string.title_notifications));
                    UserActivity userActivity = new UserActivity();
                    fragmentTransaction.replace(R.id.fragment, userActivity);
                    fragmentTransaction.commit();

                    return true;

                /*case R.id.navigation_remote_control:
                    setTitleName(getResources().getString(R.string.title_remote_control));
                    RemoteRecycleFragment remoteRecycleFragment = new RemoteRecycleFragment();
                    fragmentTransaction.replace(R.id.fragment, remoteRecycleFragment);
                    fragmentTransaction.commit();

                    return true;*/
                case R.id.navigation_monitor:
                    setTitleName("监控");
                    CameraActivity cameraActivity = new CameraActivity();
                    fragmentTransaction.replace(R.id.fragment, cameraActivity);
                    fragmentTransaction.commit();
                    return true;
            }
            return false;
        }
    };

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void setRootView() {
        super.setRootView();
        setContentView(R.layout.activity_main);

        Window window = this.getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.color_gray_2));

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        BottomNavigationHelper.disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        //初始化导航栏
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        CurrentActivity currentActivity = new CurrentActivity("006");
        currentActivity.getData("http://202.194.131.174:8080/newdongping/Wenshi_dongping2_servlet?DTU=006&num=1");
        fragmentTransaction.replace(R.id.fragment, currentActivity);
        fragmentTransaction.commit();

    }

    @Override
    protected void initWidght() {
        super.initWidght();
        setTitleName(getResources().getString(R.string.title_home));//title
//                  setTitleBack(false,R.mipmap.ic_search);//有图标，但不是返回
        //setTitleBack(true,0);//返回
//                  setTitleRightText("保存");//右侧文字

        showTitleRes(R.id.title_choose);//扩展menu（图标）
        //goneTitleRes(R.id.title_add);隐藏图标，一般用不到
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.title_back) {
            // workFragment.titleSearch();
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.title_choose://添加

                break;
            case R.id.choose_one://大棚1
                Toast.makeText(this, "大棚6", Toast.LENGTH_SHORT).show();
                FragmentManager fragmentManager1 = getFragmentManager();
                FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
                CurrentActivity currentActivity1 = new CurrentActivity("006");
                currentActivity1.getData("http://202.194.131.174:8080/newdongping/Wenshi_dongping2_servlet?DTU=006&num=1");
                fragmentTransaction1.replace(R.id.fragment, currentActivity1);
                fragmentTransaction1.commit();
                break;
            case R.id.choose_two://大棚2
                Toast.makeText(this, "大棚7", Toast.LENGTH_SHORT).show();
                FragmentManager fragmentManager2 = getFragmentManager();
                FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
                CurrentActivity currentActivity2 = new CurrentActivity("007");
                currentActivity2.getData("http://202.194.131.174:8080/newdongping/Wenshi_dongping2_servlet?DTU=007&num=1");
                fragmentTransaction2.replace(R.id.fragment, currentActivity2);
                fragmentTransaction2.commit();
                break;
            case R.id.choose_three://大棚3
                Toast.makeText(this, "大棚8", Toast.LENGTH_SHORT).show();
                FragmentManager fragmentManager3 = getFragmentManager();
                FragmentTransaction fragmentTransaction3 = fragmentManager3.beginTransaction();
                CurrentActivity currentActivity3 = new CurrentActivity("008");
                currentActivity3.getData("http://202.194.131.174:8080/newdongping/Wenshi_dongping2_servlet?DTU=008&num=1");
                fragmentTransaction3.replace(R.id.fragment, currentActivity3);
                fragmentTransaction3.commit();
                break;
            case R.id.choose_four://大棚4
                Toast.makeText(this, "大棚9", Toast.LENGTH_SHORT).show();
                FragmentManager fragmentManager4 = getFragmentManager();
                FragmentTransaction fragmentTransaction4 = fragmentManager4.beginTransaction();
                CurrentActivity currentActivity4 = new CurrentActivity("009");
                currentActivity4.getData("http://202.194.131.174:8080/newdongping/Wenshi_dongping1_servlet?DTU=009&num=1");
                fragmentTransaction4.replace(R.id.fragment, currentActivity4);
                fragmentTransaction4.commit();
                break;
            case R.id.refresh:
                FragmentManager fragmentManager0 = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager0.beginTransaction();
                CurrentActivity currentActivity0 = new CurrentActivity("006");
                currentActivity0.getData("http://202.194.131.174:8080/newdongping/Wenshi_dongping2_servlet?DTU=006&num=1");
                fragmentTransaction.replace(R.id.fragment, currentActivity0);
                fragmentTransaction.commit();
                break;

        }
        return super.onMenuItemClick(item);
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }
    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }
}
